using Renci.SshNet;
using MultiAgentsShared;

namespace MultiAgentsClient.Tools;

[GptToolSet("SSH ToolSet", "Provides functionalities to execute commands over SSH.")]
public class SshToolProvider
{
    [GptFunc("Execute SSH command and return its outputs.", true)]
    public static string ExecuteSshCommand(
        [GptFuncParam("IP address of the server")] string ipAddress,
        [GptFuncParam("Username for SSH login")] string userName,
        [GptFuncParam("Password for SSH login")] string password,
        [GptFuncParam("Command to execute over SSH")] string command,
        [GptFuncParam("Port for SSH connection")] int port = 22)
    {
        try
        {
            using var client = new SshClient(ipAddress, port, userName, password);
            client.Connect();
            if (client.IsConnected)
            {
                var cmd = client.CreateCommand(command);
                var result = cmd.Execute();
                client.Disconnect();
                return result.Trim();
            }
            else
            {
                return "Connection to SSH server failed.";
            }
        }
        catch (Exception ex)
        {
            return $"Error executing SSH command: {ex.Message}";
        }
    }
}
